package com.huawei.hms.adapter;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Util;

public class BinderAdapter implements ServiceConnection {
    private static final Object e = new Object();
    private Context a;
    private String b;
    private String c;
    private BinderCallBack d;
    private Handler f = null;

    public interface BinderCallBack {
        void onBinderFailed(int i);

        void onServiceConnected(ComponentName componentName, IBinder iBinder);

        void onServiceDisconnected(ComponentName componentName);
    }

    public BinderAdapter(Context context, String str, String str2) {
        this.a = context;
        this.b = str;
        this.c = str2;
    }

    public void unBind() {
        Util.unBindServiceCatchException(this.a, this);
    }

    public void binder(BinderCallBack binderCallBack) {
        if (binderCallBack != null) {
            this.d = binderCallBack;
            if (c()) {
                a();
                return;
            }
            HMSLog.e("BinderAdapter", "In connect, bind core service fail");
            b();
        }
    }

    private void a() {
        synchronized (e) {
            if (this.f != null) {
                this.f.removeMessages(1001);
            } else {
                this.f = new Handler(Looper.getMainLooper(), new Callback(this) {
                    final /* synthetic */ BinderAdapter a;

                    {
                        this.a = r1;
                    }

                    public boolean handleMessage(Message message) {
                        if (message == null || message.what != 1001) {
                            return false;
                        }
                        HMSLog.e("BinderAdapter", "In connect, bind core service time out");
                        this.a.b();
                        return true;
                    }
                });
            }
            this.f.sendEmptyMessageDelayed(1001, 5000);
        }
    }

    private void b() {
        BinderCallBack e = e();
        if (e != null) {
            e.onBinderFailed(-1);
        }
    }

    private boolean c() {
        if (TextUtils.isEmpty(this.b) || TextUtils.isEmpty(this.c)) {
            return false;
        }
        Intent intent = new Intent(this.b);
        intent.setPackage(this.c);
        return this.a.bindService(intent, this, 1);
    }

    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        HMSLog.i("BinderAdapter", "Enter onServiceConnected.");
        d();
        BinderCallBack e = e();
        if (e != null) {
            e.onServiceConnected(componentName, iBinder);
        }
    }

    private void d() {
        synchronized (e) {
            if (this.f != null) {
                this.f.removeMessages(1001);
                this.f = null;
            }
        }
    }

    public void onServiceDisconnected(ComponentName componentName) {
        HMSLog.i("BinderAdapter", "Enter onServiceDisconnected.");
        BinderCallBack e = e();
        if (e != null) {
            e.onServiceDisconnected(componentName);
        }
    }

    private BinderCallBack e() {
        return this.d;
    }
}
